﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmResouce : Form
    {
        private Form1 frmMain = null;
        public FrmResouce()
        {
            InitializeComponent();
        }

        public FrmResouce(Form1 _frm)
        {
            InitializeComponent();
            frmMain = _frm;
        }
       
        private void FrmResouce_Load(object sender, EventArgs e)
        {
            bindDataNew();
            //绑定段位信息
            lvSection.Clear();
            lvSection.BeginUpdate();
            lvSection.View = View.Details;
            lvSection.Columns.Add("段号", 100);

            string sql1 = "select distinct Section from (select distinct Section from UserInfo " +
                            " union select distinct Section from CarInfo) a where Section is not null and Section <>''";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                string name = reader1["Section"].ToString();
                lvSection.Items.Add(name, name, 0);
            }
            reader1.Close();
            lvSection.EndUpdate();

            //绑定未分配力量信息
            lvNo.Clear();
            lvNo.BeginUpdate();
            lvNo.View = View.Details;
            lvNo.Columns.Add("名称", 100);
            lvNo.Columns.Add("类型", 40);

            string sql2 = "select RFID, '车辆' as type,CarName as name from CarInfo where (Section='' or Section is null) and CarState='×'" +
                            " union select RFID, '官兵' as type,UserName as name from UserInfo where (Section='' or Section is null) and State='×'";
            SqlDataReader reader2 = null;
            SqlCommand cmd2 = new SqlCommand(sql2, frmMain.dbConnect);
            reader2 = cmd2.ExecuteReader();

            while (reader2.Read())
            {
                string rfid = reader2["RFID"].ToString();
                string name = reader2["name"].ToString();
                string type = reader2["type"].ToString();
                lvNo.Items.Add(rfid, name, 0);
                lvNo.Items[rfid].SubItems.Add(type);
            }
            reader2.Close();
            lvNo.EndUpdate();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string s = tbSection.Text;
            string rfid = "";
            if (s == "") return;

            //指定指挥员
            if (this.lvNo.FocusedItem != null)
            {
                rfid = this.lvNo.FocusedItem.Name;
                if (rfid == "")
                {
                    MessageBox.Show("请先在未分派力量中选定指挥员!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请先在未分派力量中选定指挥员!");
                return;
            }
            //插入数据库
            string sql2 = "update UserInfo set Section='" + s + "',IsLeader=1 where RFID='" + rfid + "'";
            SqlDataReader reader2 = null;
            SqlCommand cmd2 = new SqlCommand(sql2, frmMain.dbConnect);
            reader2 = cmd2.ExecuteReader();
            reader2.Close();
            //增加列表数据
            lvSection.Items.Add(s, s, 0);
            lb.Text = s;
        }

        private void lvSection_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定已选择力量
             string name = "";
             if (this.lvSection.FocusedItem != null)
             {
                 name = this.lvSection.FocusedItem.SubItems[0].Text;
                 lb.Text = name;
                 lvYes.Clear();
                 lvYes.BeginUpdate();
                 lvYes.View = View.Details;
                 lvYes.Columns.Add("名称", 100);
                 lvYes.Columns.Add("类型", 40);

                 string sql2 = "select * from (select RFID, '车辆' as type,CarName as name,null as leader from CarInfo where Section='" + name + "' and CarState='×'" +
                                 " union select RFID,'官兵' as type,UserName as name,isLeader as leader from UserInfo where Section='" + name + "' and State='×' ) a order by leader desc";
                 SqlDataReader reader2 = null;
                 SqlCommand cmd2 = new SqlCommand(sql2, frmMain.dbConnect);
                 reader2 = cmd2.ExecuteReader();

                 while (reader2.Read())
                 {
                     name = reader2["name"].ToString();
                     string rfid = reader2["RFID"].ToString();
                     string type = reader2["type"].ToString();
                     string leader = reader2["leader"].ToString();
                     lvYes.Items.Add(rfid, name, 0);
                     if (leader =="1")
                         lvYes.Items[rfid].SubItems.Add("指挥员");
                     else
                         lvYes.Items[rfid].SubItems.Add(type);
                 }
                 reader2.Close();
                 lvYes.EndUpdate();
             }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (lb.Text == "")
            {
                MessageBox.Show("请先选定段!");
                return;
            }
            int Index;
            string rfid, type, name = "";
            if (this.lvNo.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.lvNo.SelectedItems[0].Index;
                rfid = this.lvNo.FocusedItem.Name;
                name = this.lvNo.FocusedItem.SubItems[0].Text;
                type = this.lvNo.FocusedItem.SubItems[1].Text;
                if (rfid == "")
                {
                    MessageBox.Show("请先在未分派力量列表中选定待分配的力量!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请先在未分派力量列表中选定待分配的力量!");
                return;
            }
            if (type == "")
            {
                MessageBox.Show("请先在未分派力量列表中选定待分配的力量!");
                return;
            }
            
            //插入数据库
            string sql2 = "";
            if (type == "车辆")
                sql2 = "update CarInfo set Section ='" + lb.Text + "' where RFID='" + rfid + "'";
            else
                sql2 = "update UserInfo set Section='" + lb.Text + "',IsLeader=0 where RFID='" + rfid + "'";
             
            SqlDataReader reader2 = null;
            SqlCommand cmd2 = new SqlCommand(sql2, frmMain.dbConnect);
            reader2 = cmd2.ExecuteReader();
            reader2.Close();
            //增加列表数据
            lvYes.Items.Add(rfid, name, 0);
            lvYes.Items[rfid].SubItems.Add(type);
            //删除待选列表
            lvNo.Items[Index].Remove();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.lvYes.SelectedItems.Count > 0)//判断listview有被选中项
            {
                int Index = this.lvYes.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
                //删除数据库中的数据
                string rfid, type, name = "";
                rfid = lvYes.SelectedItems[0].Name;
                type = lvYes.SelectedItems[0].SubItems[1].Text;
                name = lvYes.SelectedItems[0].SubItems[0].Text;
                if (rfid == "")
                {
                    MessageBox.Show("请先在已分派力量中选择要删除的力量!");
                    return;
                }
                string sql2;
                if(type=="车辆")
                    sql2 = "update CarInfo set Section='' where RFID='" + rfid + "'";
                else
                    sql2 = "update UserInfo set Section='',IsLeader=null where RFID='" + rfid + "'";
                SqlDataReader reader2 = null;
                SqlCommand cmd2 = new SqlCommand(sql2, frmMain.dbConnect);
                reader2 = cmd2.ExecuteReader();
                reader2.Close();
                //未分配力量列表增加
                lvNo.Items.Add(rfid, name, 0);
                lvNo.Items[rfid].SubItems.Add(type);
                //已分配列表减少
                lvYes.Items[Index].Remove();
            }
            else
            {
                MessageBox.Show("请先在已分派力量中选择要删除的力量!");
            }
        }

        private void lvYes_MouseDown(object sender, MouseEventArgs e)
        {
            string rfid = "";
            string name = "";
            string type = "";

            if (this.lvYes.SelectedItems.Count > 0)//判断listview有被选中项
            {
                int Index = this.lvYes.SelectedItems[0].Index;
               
                rfid = lvYes.SelectedItems[0].Name;
                type = lvYes.SelectedItems[0].SubItems[1].Text;
                name = lvYes.SelectedItems[0].SubItems[0].Text;
                if (rfid == "")
                {
                    //MessageBox.Show("请先在已分派力量中选择要的力量!");
                    return;
                }
                if (type == "车辆")//duanhao
                    type = "car";
                else
                    type = "user";
            }
            name = "1;指挥员A;作战任务...";
            type = "duanhao";
            frmMain.newType = ImageType.carDrafting;
            frmMain.UiDrafting_MouseDown(name, type);
        }

        //绑定各区已选择力量
        private void bindDataNew()
        {
            listView111.Clear();
            listView111.BeginUpdate();
            listView111.View = View.Details;
            listView111.FullRowSelect = true;
            listView111.Columns.Add("段号", 50);
            listView111.Columns.Add("指挥员", 50);
            listView111.Columns.Add("官兵", 100);
            listView111.Columns.Add("车辆", 100);
            listView111.Columns.Add("作战任务", 100);
            listView111.Columns.Add("...", 100);

            //绑定段位信息
            //listView1.Clear();
            string sql1 = "select distinct Section from (select distinct Section from UserInfo " +
                            " union select distinct Section from CarInfo) a where Section is not null and Section <>''";
            SqlCommand cmd = new SqlCommand(sql1, frmMain.dbConnect);
            SqlDataAdapter datapter01 = new SqlDataAdapter(cmd);
            DataSet ds01 = new DataSet();
            datapter01.Fill(ds01);
            for (int j = 0; j < ds01.Tables[0].Rows.Count; j++)
            {
                string section = ds01.Tables[0].Rows[j]["Section"].ToString();
                string sql = "select * from (select RFID,'指挥员' as type,UserName as name,isLeader as leader ,section " +
                    "from UserInfo where State='×' and isLeader=1  and section='" + section + "' union " +
                    "select RFID,'官兵' as type,UserName as name,isLeader as leader ,section " +
                    "from UserInfo where State='×' and isLeader=0  and section='" + section + "' union " +
                    "select RFID, '车辆' as type,CarName as name,0 as leader ,section " +
                    "from CarInfo where CarState='×'  and section='" + section + "' ) a  order by leader desc ";
                SqlDataReader reader1 = null;
                SqlCommand cmd1 = new SqlCommand(sql, frmMain.dbConnect);
                reader1 = cmd1.ExecuteReader();
                string user1 = "";
                string user2 = "";
                string car1 = "";
                string rfid = "";
                while (reader1.Read())
                {
                    string name = reader1["name"].ToString();
                    rfid = reader1["RFID"].ToString();
                    string leader = reader1["leader"].ToString();
                    string type = reader1["type"].ToString();

                    if (type == "指挥员")
                        user1 = name;
                    if (type == "官兵")
                        user2 = name + "," + user2;
                    if (type == "车辆")
                        car1 = name + "," + car1;
                }
                if (user1 == "")
                    user1 = "无指挥员";
                user2 = user2.Substring(0, user2.Length - 1);
                car1 = car1.Substring(0, car1.Length - 1);

                listView111.Items.Add(j.ToString(), "", j + 1);
                listView111.Items[j.ToString()].SubItems.Add(user1);
                listView111.Items[j.ToString()].SubItems.Add(user2);
                listView111.Items[j.ToString()].SubItems.Add(car1);
                listView111.Items[j.ToString()].SubItems.Add("...");
                listView111.Items[j.ToString()].SubItems.Add("...");
                reader1.Close();
            }
            
            datapter01.Dispose();
            listView111.EndUpdate();
        }

        //绑定各区已选择力量
        private void bindData()
        {
            //panelParent.Controls.Clear();

            //listView111.Clear();
            //listView111.BeginUpdate();
            //listView111.View = View.Details;
            //listView111.FullRowSelect = true;
            //listView111.Columns.Add("段号", 50);
            //listView111.Columns.Add("指挥员", 100);
            //listView111.Columns.Add("官兵", 100);
            //listView111.Columns.Add("车辆", 100);
            //listView111.Columns.Add("作战任务", 100);
            //listView111.Columns.Add("...", 100);

            ////绑定段位信息
            ////listView1.Clear();
            //string sql1 = "select distinct Section from (select distinct Section from UserInfo " +
            //                " union select distinct Section from CarInfo) a where Section is not null and Section <>''";
            //SqlCommand cmd = new SqlCommand(sql1, frmMain.dbConnect);                        
            //SqlDataAdapter datapter01 = new SqlDataAdapter(cmd);
            //DataSet ds01 = new DataSet();
            //datapter01.Fill(ds01);
            //for (int j = 0; j < ds01.Tables[0].Rows.Count; j++)
            //{
            //    string section = ds01.Tables[0].Rows[j]["Section"].ToString();            
           
            //    //动态增加一个段的Panel
            //    Panel pan = new Panel();
            //    pan.Dock = DockStyle.Top;
            //    pan.Size = new System.Drawing.Size(52, 108);

            //    if (j > 1) break;
            //    //数字
            //    Panel pannumber = new Panel();
            //    pannumber.Dock = DockStyle.Left;
            //    pannumber.Size = new System.Drawing.Size(52, 98);
                
            //    //pic
            //    PictureBox pb = new PictureBox();
            //    pb.BackgroundImage = imageList1.Images[j + 1];
            //    pb.BackgroundImageLayout = ImageLayout.Center;
            //    pb.Size = new System.Drawing.Size(40, 40);
                
            //    pannumber.Controls.Add(pb);
            //    pan.Controls.Add(pannumber);

            //    //panellistview
            //    Panel panlist = new Panel();
            //    panlist.Dock = DockStyle.Fill;

            //    //listview
            //    ListView lv = new ListView();
            //    lv.Dock = DockStyle.None;
            //    lv.Name = "listView" + (j+1).ToString();
            //    lv.Size = new System.Drawing.Size(500, 400);
            //    lv.View = View.Details;
            //    //lv.GridLines = true;
            //    lv.FullRowSelect = true;
            //    //lv.Scrollable = true;
            //    //lv.MultiSelect = false;
            //    //lv.Visible = true;

            //    lv.BeginUpdate();
            //    lv.Columns.Add("指挥员", 100);
            //    lv.Columns.Add("官兵", 100);
            //    lv.Columns.Add("车辆", 100);
            //    lv.Columns.Add("作战任务", 100);
            //    lv.Columns.Add("...", 100);

            //    string sql = "select * from (select RFID,'指挥员' as type,UserName as name,isLeader as leader ,section " +
            //        "from UserInfo where State='×' and isLeader=1  and section='" + section + "' union " +
            //        "select RFID,'官兵' as type,UserName as name,isLeader as leader ,section " +
            //        "from UserInfo where State='×' and isLeader=0  and section='" + section + "' union " +
            //        "select RFID, '车辆' as type,CarName as name,0 as leader ,section " +
            //        "from CarInfo where CarState='×'  and section='" + section + "' ) a  order by leader desc ";
            //    SqlDataReader reader1 = null;
            //    SqlCommand cmd1 = new SqlCommand(sql, frmMain.dbConnect);
            //    reader1 = cmd1.ExecuteReader();
            //    string user1 = "";
            //    string user2 = "";
            //    string car1 = "";
            //    string rfid = "";
            //    while (reader1.Read())
            //    {
            //        string name = reader1["name"].ToString();
            //        rfid = reader1["RFID"].ToString();
            //        string leader = reader1["leader"].ToString();
            //        string type = reader1["type"].ToString();

            //        if (type == "指挥员")
            //            user1 = name;
            //        if (type == "官兵")
            //            user2 = name + "," + user2;
            //        if (type == "车辆")
            //            car1 = name + "," + car1;
            //    }
            //    if (user1 == "")
            //        user1 = "无指挥员";

            //    //ListViewItem Item = new ListViewItem();
            //    //Item.Text = user1;
            //    //Item.SubItems[0].Text = user2;
            //    //Item.SubItems[1].Text = car1;
            //    //Item.SubItems[2].Text = "...";
            //    //Item.SubItems[3].Text = "...";
            //    //lv.Items.Add(Item);

            //    //lv.Items.Add(user1);
            //    //lv.Items[j].SubItems.Add(user2);
            //    //lv.Items[j].SubItems.Add(car1);
            //    //lv.Items[j].SubItems.Add("...");
            //    //lv.Items[j].SubItems.Add("...");

            //    lv.Items.Add(user1, user1, 0);
            //    lv.Items[user1].SubItems.Add(user2);
            //    lv.Items[user1].SubItems.Add(car1);
            //    lv.Items[user1].SubItems.Add("...");
            //    lv.Items[user1].SubItems.Add("...");

            //    if (j >= 0)
            //    {
                    
            //        listView111.Items.Add(j.ToString(), "", 0);
            //        //listView111.Items.Add(user1, user1, 0);
            //        listView111.Items[j.ToString()].SubItems.Add(user1);
            //        listView111.Items[j.ToString()].SubItems.Add(user2);
            //        listView111.Items[j.ToString()].SubItems.Add(car1);
            //        listView111.Items[j.ToString()].SubItems.Add("...");
            //        listView111.Items[j.ToString()].SubItems.Add("...");
            //    }
            //    lv.EndUpdate();
            //    panlist.Controls.Add(lv);
            //    pan.Controls.Add(panlist);
            //    panelParent.Controls.Add(pan);
            //    panelParent.AutoScroll = true;
            //    reader1.Close();
            //}
            //datapter01.Dispose();
            //listView111.EndUpdate();
        }


        //private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    frmMain.newType = ImageType.carDrafting;
        //    frmMain.UiDrafting_MouseDown("1", "imgOrder");
        //}

        private void listView111_MouseDown(object sender, MouseEventArgs e)
        {
            listView111_Click(sender,e);
            int j = 0;
            if (this.listView111.FocusedItem != null)
            {
                j = this.listView111.FocusedItem.ImageIndex;
            }
            string name = j.ToString()+";指挥员A;作战任务...";
            frmMain.newType = ImageType.carDrafting;
            frmMain.UiDrafting_MouseDown(name,"duanhao");
        }

        private void listView111_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView111_Click(object sender, EventArgs e)
        {
            return;
        }

        private void FrmResouce_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmResouce", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmResouce", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
